package com.polaris.live.ui.party.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.polaris.live.adapter.party.GameSeatAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.databinding.WidgetPartyPlayGameSeatBinding
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.ui.party.listener.GameSeatViewListener
import com.polaris.live.ui.party.widget.seat.bean.GameSeatBean

/**
 * 游戏等待座位
 * @author Created by 天晴 on 2024/3/13 17:24
 * @since 1.0.0
 **/
class GameSeatView(context: Context, attrs: AttributeSet?) :
    FrameLayout(context, attrs) {

    private val mBinding =
        WidgetPartyPlayGameSeatBinding.inflate(LayoutInflater.from(context), this)

    private val mLayoutManager by lazy {
        LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
    }
    private val mAdapter by lazy {
        GameSeatAdapter()
    }

    /**
     * 自己的位置
     */
    private var meIndex: Int? = null
    private var onGameSeatViewListener: GameSeatViewListener? = null

    init {
        mBinding.gameRecycler.apply {
            adapter = mAdapter
            layoutManager = mLayoutManager
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            val data = mAdapter.getItemOrNull(position)
            onGameSeatViewListener?.clickItem(data?.userBasicVo?.userId)
        }
    }

    fun getMeGameSeatId(): Int? {
        return meIndex
    }

    fun getUsers(): List<String> {
        return mAdapter.data
            .filter { it.userBasicVo != null }
            .map { it.userBasicVo?.userId.toString() }
    }

    fun setOnGameSeatViewListener(linstener: GameSeatViewListener) {
        onGameSeatViewListener = linstener
    }

    fun upLoadedItem(item: Int, isload: Boolean) {
        mAdapter.getItemOrNull(item)?.apply {
            this.isLoaded = isload
            mAdapter.notifyItemChanged(item)
        }

    }

    fun upDataItem(item: Int, mUserBasicVo: UserBasicVo?) {
        mAdapter.getItemOrNull(item)?.apply {
            if (item == meIndex) {
                if (mUserBasicVo == null || mUserBasicVo.userId != UserManager.getUserId()) {
                    meIndex = null
                    onGameSeatViewListener?.meChange(null)
                }
            } else if (mUserBasicVo != null && mUserBasicVo.userId == UserManager.getUserId()) {
                meIndex = item
                onGameSeatViewListener?.meChange(meIndex)
            }
            userBasicVo = mUserBasicVo
            mAdapter.notifyItemChanged(item)
        }

    }

    /**
     * 设置游戏位数量
     */
    fun setSeatCount(count: Int) {
        assert(count >= 0)
        mAdapter.data.clear()
        val list = List(count) { index -> GameSeatBean(index) }.toMutableList()
        mAdapter.addData(list)
    }

}